package cn.com.thingo.shyintelligent.presenter;

import com.videogo.openapi.EZOpenSDK;
import com.videogo.util.MD5Util;

import org.reactivestreams.Publisher;

import javax.inject.Inject;

import cn.com.thingo.shyintelligent.app.App;
import cn.com.thingo.shyintelligent.app.EzHttpConstants;
import cn.com.thingo.shyintelligent.base.RxPresenter;
import cn.com.thingo.shyintelligent.base.contract.LoginContract;
import cn.com.thingo.shyintelligent.model.DataManager;
import cn.com.thingo.shyintelligent.model.bean.EzAccessTokenBean;
import cn.com.thingo.shyintelligent.model.bean.EzSubAccountBean;
import cn.com.thingo.shyintelligent.model.bean.EzSubAccountIdBean;
import cn.com.thingo.shyintelligent.model.bean.LoginBean;
import cn.com.thingo.shyintelligent.model.db.UserInfo;
import cn.com.thingo.shyintelligent.model.http.api.EzApis;
import cn.com.thingo.shyintelligent.util.EzHttpUtils;
import cn.com.thingo.shyintelligent.util.LogUtil;
import cn.com.thingo.shyintelligent.util.RxUtil;
import cn.com.thingo.shyintelligent.util.ToastUtil;
import io.reactivex.Flowable;
import io.reactivex.Observable;
import io.reactivex.ObservableSource;
import io.reactivex.functions.Consumer;
import io.reactivex.functions.Function;

public class LoginPresenter extends RxPresenter<LoginContract.View> implements LoginContract.Presenter {


    private DataManager dataManager;
    private UserInfo mUserInfo;
    private EzApis mEzApis;

    @Inject
    public LoginPresenter(DataManager dataManager) {
        this.dataManager = dataManager;
        mUserInfo = UserInfo.getInstance();
        mEzApis = EzHttpUtils.getInstance().getEzApis();
    }

    @Override
    public void login(final String password, final String telephone) {
        addSubscribe(dataManager.login(password, telephone)
                .compose(RxUtil.<LoginBean>rxSchedulerHelper())
                .subscribe(new Consumer<LoginBean>() {
                    @Override
                    public void accept(final LoginBean loginBean) throws Exception {
                        if (loginBean.getCode() == 0) {
                            final String md5AccountName = MD5Util.getMD5String(telephone).toLowerCase();
                            addSubscribe(mEzApis.postEzAccessTokenBean(App.EZ_APP_Key, App.EZ_SECRET).flatMap(new Function<EzAccessTokenBean, Flowable<EzSubAccountBean>>() {
                                @Override
                                public Flowable<EzSubAccountBean> apply(EzAccessTokenBean ezAccessTokenBean) throws Exception {
                                    if (ezAccessTokenBean.getCode().equals(EzHttpConstants.OPERATE_SUCCESSFULLY_CODE)) {
                                        String ezAccessToken = ezAccessTokenBean.getData().getAccessToken();
                                        mUserInfo.setAccessToken(ezAccessToken);
                                    }
                                    return mEzApis.postEzSubAccountBean(mUserInfo.getAccessToken(), null, md5AccountName);
                                }
                            }).compose(RxUtil.<EzSubAccountBean>rxSchedulerHelper()).subscribe(new Consumer<EzSubAccountBean>() {
                                @Override
                                public void accept(final EzSubAccountBean ezSubAccountBean) throws Exception {
                                    String code = ezSubAccountBean.getCode();
                                    switch (code) {
                                        case EzHttpConstants.SUBACCOUNT_NO_EXIST_CODE:
                                            String md5AccountName = MD5Util.getMD5String(telephone).toLowerCase();
                                            String md5Password = MD5Util.getMD5String(password).toLowerCase();
                                            addSubscribe(mEzApis.postCreateSubAccount(mUserInfo.getAccessToken(), md5AccountName, md5Password).flatMap(new Function<EzSubAccountIdBean, Flowable<EzAccessTokenBean>>() {
                                                @Override
                                                public Flowable<EzAccessTokenBean> apply(EzSubAccountIdBean ezSubAccountIdBean) throws Exception {
                                                    if (ezSubAccountBean.getCode().equals(EzHttpConstants.OPERATE_SUCCESSFULLY_CODE)) {
                                                        mUserInfo.setAccountId(ezSubAccountIdBean.getData().getAccountId());
                                                    }
                                                    return mEzApis.postEzSubAccessTokenBean(mUserInfo.getAccessToken(), mUserInfo.getAccountId());
                                                }
                                            }).compose(RxUtil.<EzAccessTokenBean>rxSchedulerHelper()).subscribe(new Consumer<EzAccessTokenBean>() {
                                                @Override
                                                public void accept(EzAccessTokenBean ezAccessTokenBean) throws Exception {
                                                    if (ezAccessTokenBean.getCode().equals(EzHttpConstants.OPERATE_SUCCESSFULLY_CODE)) {
                                                        mUserInfo.setRamAccessToken(ezAccessTokenBean.getData().getAccessToken());
                                                        EZOpenSDK.getInstance().setAccessToken(mUserInfo.getRamAccessToken());
                                                        mView.viewLogin(loginBean);
                                                    } else {
                                                        ToastUtil.shortShow(ezAccessTokenBean.getMsg());
                                                    }
                                                }
                                            }));
                                            break;
                                        case EzHttpConstants.OPERATE_SUCCESSFULLY_CODE:
                                           mUserInfo.setAccountId(ezSubAccountBean.getData().getAccountId());
                                            addSubscribe(mEzApis.postEzSubAccessTokenBean(mUserInfo.getAccessToken(), mUserInfo.getAccountId()).compose(RxUtil.<EzAccessTokenBean>rxSchedulerHelper()).subscribe(new Consumer<EzAccessTokenBean>() {
                                                @Override
                                                public void accept(EzAccessTokenBean ezAccessTokenBean) throws Exception {
                                                    if (ezAccessTokenBean.getCode().equals(EzHttpConstants.OPERATE_SUCCESSFULLY_CODE)) {
                                                        mUserInfo.setRamAccessToken(ezAccessTokenBean.getData().getAccessToken());
                                                        EZOpenSDK.getInstance().setAccessToken(mUserInfo.getRamAccessToken());
                                                        mView.viewLogin(loginBean);
                                                    } else {
                                                        ToastUtil.shortShow(ezAccessTokenBean.getMsg());
                                                    }
                                                }
                                            }));
                                            break;

                                    }
                                }
                            }));

                        } else {
                            ToastUtil.shortShow(loginBean.getMsg());
                        }
                    }
                }, new Consumer<Throwable>() {
                    @Override
                    public void accept(Throwable throwable) throws Exception {
                        LogUtil.e(throwable.getMessage());
                    }
                }));
    }


}
